package com.eassy.controller;

import com.eassy.bean.Admin;
import com.eassy.config.Result;
import com.eassy.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("admin")
//允许跨域
@CrossOrigin
public class Admincontroller {
    //依赖注入
//    @Autowired
//    AdminService adminService;
//    @RequestMapping("/list")
//    public List<Admin> getlist(){
//        List<Admin> list = adminService.list();
//        return  list;
//    }
    @Autowired
    AdminService adminService;
    @GetMapping("list")
    public Result getlist(){
        List<Admin> list = adminService.list();
        return Result.success(list);
    }
    @PostMapping( "add")
    public Result add(@RequestBody Admin admin){
        adminService.save(admin);
        Admin adminService1 = adminService.getById(admin.getId());
        return Result.success("保存成功",adminService1);
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") int id){
       boolean b=adminService.removeById(id);
       if(b){
           return Result.success("删除成功");
       }else{
           return Result.fail(201,"删除失败");
       }
    }
    @PutMapping("update")
    public  Result edit(@RequestBody Admin admin){
        boolean b = adminService.updateById(admin);
        Admin admin1=adminService.getById(admin.getId());
        if(b){
            return Result.success("修改成功");
        }else{
            return Result.fail(201,"修改失败");
        }
    }
}
